package com.example.clock.planPage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.clock.R;
import com.example.clock.basicClass.HelpFunc;
import com.example.clock.basicClass.Plan;
import com.example.clock.basicClass.Tags;
import com.example.clock.controlClass.DatabaseHelper;
import com.example.clock.controlClass.SqlController;

import java.lang.reflect.Array;


public class NewPlan extends AppCompatActivity {
    Plan plan;
    String[] bigTags;
    String[][] smallTags;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_plan);


        DatabaseHelper dbhelper = new DatabaseHelper(NewPlan.this,NewPlan.this);
        SQLiteDatabase db = dbhelper.getWritableDatabase();
        bigTags = Tags.getStringBigTags(db);
        smallTags = Tags.getStringSmallTags(db);


        plan = new Plan("新的计划");


        //spinner标签设置
        ArrayAdapter<String> bigAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,bigTags);
        ArrayAdapter<String> smallAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,HelpFunc.deleteEmptyElementinArray(smallTags[0]));
        Spinner sp1 = findViewById(R.id.spinner);
        sp1.setAdapter(bigAdapter);
        Spinner sp2 = findViewById(R.id.spinner2);
        sp2.setAdapter(smallAdapter);


        sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // 更改小标签
                Spinner sp2 = findViewById(R.id.spinner2);
                ArrayAdapter<String> smallAdapter = new ArrayAdapter<String>(NewPlan.this,android.R.layout.simple_spinner_item,HelpFunc.deleteEmptyElementinArray(smallTags[position]));
                sp2.setAdapter(smallAdapter);
                //设置大标签
                plan.setLargeTags(parentView.getItemAtPosition(position).toString());
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // 当没有选中任何项时触发
            }
        });

        sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // 设置小标签
                plan.setSmallTags(parentView.getItemAtPosition(position).toString());
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // 当没有选中任何项时触发
            }
        });


    }

    public void newPlan(View view){
        //获取plan的相关数据

        EditText e1 = (EditText) findViewById(R.id.editText3);
        EditText e2 = (EditText) findViewById(R.id.editText2);
        EditText e3 = (EditText) findViewById(R.id.editText4);
        EditText e4 = (EditText) findViewById(R.id.editText5);
        EditText e5 = (EditText) findViewById(R.id.editText6);

        if(
                  HelpFunc.checkForNullFormat(NewPlan.this,e1.getText().toString())
                &&HelpFunc.checkForNullFormat(NewPlan.this,e2.getText().toString())
                &&HelpFunc.checkForNullFormat(NewPlan.this,e3.getText().toString())
                &&HelpFunc.checkForNullFormat(NewPlan.this,e4.getText().toString())
                &&HelpFunc.checkForNullFormat(NewPlan.this,e5.getText().toString())
                &&HelpFunc.checkForDateFormat(NewPlan.this,e2.getText().toString())
                &&HelpFunc.checkForDateFormat(NewPlan.this,e3.getText().toString())){
            //符合条件
            plan.setPlanName(e1.getText().toString());
            plan.setStartDate(e2.getText().toString());
            plan.setEndDate(e3.getText().toString());
            plan.setPreTimes(Integer.parseInt(e4.getText().toString()));
            plan.setLastTimeByTimes((Integer.parseInt(e5.getText().toString())));
            plan.setFinishTimes(0);
            plan.setLastTime(0);

            //存入数据库并退出
            DatabaseHelper dbhelper = new DatabaseHelper(NewPlan.this,NewPlan.this);
            SQLiteDatabase db = dbhelper.getWritableDatabase();
            SqlController.addPlan(db,plan);
            finish();

        }

    }
    public void calcelNewPlan(View view){
        finish();
    }

    public void changeText(View view){

        TextView t = view.findViewById(R.id.textView29);
    }
}